/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.data.feature.extaction;

import java.io.Serializable;

import java.util.List;

import phoside.data.feature.model.Instance;
import phoside.data.feature.model.InstanceUtil;

import phoside.util.ProteinSequenceUtil;
import phoside.util.StaticFinalValues;

/**
 *
 * @author gjj
 */
public class AminoAcidFrequencyFeatureExtractor implements FeatureExtractor, Serializable  {
    private static final long serialVersionUID = -6507052020284439316L;
    
    protected String aminoAcidAlphabet;
    protected final int windowOffset;

    public AminoAcidFrequencyFeatureExtractor(final int windowOffset) {
        this(windowOffset, null);
    }

    public AminoAcidFrequencyFeatureExtractor(final int windowOffset,
                                              final String aminoAcidAlphabet) {
        if (windowOffset<0) {
            throw new IllegalArgumentException("Window size cannot be negative.");
        }

        if (aminoAcidAlphabet==null) {
            this.aminoAcidAlphabet = ProteinSequenceUtil.ALPHABET;
        } else {
            this.aminoAcidAlphabet = aminoAcidAlphabet;
        }
        
        this.windowOffset = windowOffset;
    }

    public void setAminoAcidAlphabet(String aminoAcidAlphabet) {
        if (aminoAcidAlphabet==null) {
            throw new NullPointerException();
        }

        this.aminoAcidAlphabet = aminoAcidAlphabet;
    }

    /**
     * Extract features from protein at site and save to instance
     * @param protein
     * @param site
     * @param savetoInstance
     */
    public List<Double> extract(final Instance instance, final boolean save) {
        if (instance==null) {
            throw new NullPointerException();
        }

        String sequence = InstanceUtil.extractSurroundingSequence(instance, 
                windowOffset, true);
        if (sequence==null)
            return null;

        StringBuilder sb = new StringBuilder(sequence);
        sb.deleteCharAt(windowOffset); // remove the center

        //String noX = sequence.replaceAll("[^"+ProteinSequenceUtil.ALPHABET+"]", "");
        List<Double> features = ProteinSequenceUtil.aminoAcidFrequencies(
                sb.toString(), aminoAcidAlphabet);

        if (save) {
            instance.putFeatures(featureTag(), features);
        }

        return features;
    }

    /**
     *
     * @return
     */
    public String featureTag() {
        return StaticFinalValues.AAFREQUENCY;
    }
}
